//
//  FANOriginalView.m
//  Weibo - OC
//
//  Created by user on 16/4/21.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANOriginalView.h"

//#warning 宏定义一定要放到 导入头文件的 上面, 会影响编译

//define this constant if you want to use Masonry without the 'mas_' prefix

#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax

#define MAS_SHORTHAND_GLOBALS

#define kMargin 10
#define ImageWH 35

#import <Masonry/Masonry.h>
#import <UIImageView+WebCache.h>
#import "FANUser.h"
#import "FANPictureCollectionView.h"
#import <YYText/YYText.h>

@interface FANOriginalView ()

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UIImageView *memberImageView;

@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) UILabel *sourceLabel;

@property (nonatomic, strong) YYLabel *contentLabel;

@property (nonatomic, strong) FANPictureCollectionView *pictureView;

@property (nonatomic, strong) MASConstraint *picBottomCons;


@end

@implementation FANOriginalView

/*
 @property (nonatomic, copy) NSString *profile_image_url;
 
 @property (nonatomic, copy) NSString *name;
 
 @property (nonatomic, copy) NSNumber *mbrank;
 
 @property (nonatomic, copy) NSString *text;
 
 @property (nonatomic, copy) NSString *created_at;
 
 @property (nonatomic, copy) NSString *source;
 
 */



- (void)setStatusViewModel:(FANStatusViewModel *)statusViewModel{
    
    _statusViewModel = statusViewModel;
    
    NSString *iconImageName = statusViewModel.status.user.profile_image_url;
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:iconImageName] placeholderImage:[UIImage imageNamed:@"avatar_default_small"]];
    
    if (statusViewModel.memberImage) {
        self.memberImageView.image = [UIImage imageNamed:statusViewModel.memberImage];

    }

    self.nameLabel.text = statusViewModel.status.user.name;
    
//    self.timeLabel.text = statusViewModel.status.created_at;
    
    self.timeLabel.text = statusViewModel.createText;
    
    self.sourceLabel.text = statusViewModel.sourceText;
    
//    self.contentLabel.text = statusViewModel.status.text;
    
    self.contentLabel.attributedText = statusViewModel.originalAttributString;
    
    [self.picBottomCons uninstall];
    
    if (statusViewModel.status.pic_urls && statusViewModel.status.pic_urls.count > 0) {
        
        if (statusViewModel.status.pic_urls.count == 1) {
            
            UICollectionViewFlowLayout *layout =(UICollectionViewFlowLayout *)self.pictureView.collectionViewLayout;
            
            layout.itemSize = statusViewModel.originalPictureSize;
            
        }else{
            
            UICollectionViewFlowLayout *layout =(UICollectionViewFlowLayout *)self.pictureView.collectionViewLayout;
            
            layout.itemSize = CGSizeMake(FANPictureWH, FANPictureWH);
        }
        
        [self.pictureView updateConstraints:^(MASConstraintMaker *make) {
            
            make.size.equalTo(statusViewModel.originalPictureSize);
        }];
        
        self.pictureView.hidden = false;
        
        self.pictureView.pic_urls = statusViewModel.status.pic_urls;
        
        [self makeConstraints:^(MASConstraintMaker *make) {
            
            self.picBottomCons = make.bottom.equalTo(self.pictureView.bottom).offset(FANMargin);
        }];
        
    }else{
        
        self.pictureView.hidden = true;
        
        [self makeConstraints:^(MASConstraintMaker *make) {
            
            self.picBottomCons = make.bottom.equalTo(self.contentLabel.bottom).offset(FANMargin);
        }];
    }
    
}

- (FANPictureCollectionView *)pictureView{
    
    if (!_pictureView) {
        
        _pictureView = [[FANPictureCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    }
    
    return _pictureView;
}

- (UIImageView *)iconImageView{
    
    if (!_iconImageView) {
        
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_small"]];
    }
    
    return _iconImageView;
}

- (UILabel *)nameLabel{
    
    if (!_nameLabel) {
        
        _nameLabel = [[UILabel alloc] init];
        
        _nameLabel.text = @"哈哈哈";
        
        _nameLabel.font = [UIFont systemFontOfSize:14];
        
        _nameLabel.textColor = [UIColor darkGrayColor];
    }
    
    return _nameLabel;
}

- (UIImageView *)memberImageView{
    
    if (!_memberImageView) {
        
        _memberImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_membership_expired"]];
    }
    return _memberImageView;
}

- (UILabel *)timeLabel{
    
    if (!_timeLabel) {
        
        _timeLabel = [[UILabel alloc] init];
        
        _timeLabel.text = @"刚刚";
        
        _timeLabel.font = [UIFont systemFontOfSize:11];
        
        _timeLabel.textColor = [UIColor darkGrayColor];
    }
    
    return _timeLabel;
}

- (UILabel *)sourceLabel{
    
    if (!_sourceLabel) {
        
        _sourceLabel = [[UILabel alloc] init];
        
        _sourceLabel.text = @"来自 iphone";
        
        _sourceLabel.textColor = [UIColor darkGrayColor];
        
        _sourceLabel.font = [UIFont systemFontOfSize:11];
    }
    
    return _sourceLabel;
}

- (YYLabel *)contentLabel{
    
    if (!_contentLabel) {
        
        _contentLabel = [[YYLabel alloc] init];
    
        _contentLabel.textColor = [UIColor darkGrayColor];
        
        _contentLabel.font = [UIFont systemFontOfSize:15];
        
        _contentLabel.numberOfLines = 5;
        
        _contentLabel.userInteractionEnabled = true;
        
        _contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 2 * kMargin;
    }
    
    return _contentLabel;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.iconImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.memberImageView];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.pictureView];

    [self.iconImageView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.leading.equalTo(self).offset(kMargin);
        
        make.width.height.equalTo(ImageWH);
        
    }];
    
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.iconImageView.top);
        
        make.leading.equalTo(self.iconImageView.trailing).offset(kMargin);
        
    }];
    
    [self.memberImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.nameLabel.centerY);
        
        make.leading.equalTo(self.nameLabel.trailing).offset(kMargin);
        
    }];
    
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(self.iconImageView.bottom);
        
        make.leading.equalTo(self.iconImageView.trailing).offset(kMargin);
        
    }];
    
    [self.sourceLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.timeLabel.bottom);
        
        make.leading.equalTo(self.timeLabel.trailing).offset(kMargin);
        
    }];
    
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.iconImageView.bottom).offset(kMargin);
        
        make.leading.equalTo(self).offset(kMargin);
        
        make.trailing.equalTo(self).offset(-kMargin);

    }];
    
    [self.pictureView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentLabel.bottom).offset(FANMargin);
        
        make.leading.equalTo(self.contentLabel.leading);
        
//        make.size.equalTo(100);
        
    }];

    [self makeConstraints:^(MASConstraintMaker *make) {
        
       self.picBottomCons = make.bottom.equalTo(self.pictureView.bottom).offset(kMargin);
        
    }];
    
}

@end
